#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

#include "read_server_model.hpp"
#include "../../comm/util.hpp"

namespace ns_view
{
    using namespace ns_log;
    using namespace ns_model;
    class view
    {
        const std::string template_path = "./template_html/";

    public:
        view()
        {
        }
        ~view()
        {
        }

    public:
        void oneExpandHtml(const chapter &chp, std::string *html)
        {
            // 章节名 章节总字数 章节正文
            // 1. 形成路径
            std::string src_html = template_path + "chapter_read.html";

            // 2. 形成数字字典
            ctemplate::TemplateDictionary root("chapter");
            root.SetValue("chapter_id", chp._chapter_id);
            root.SetValue("chapter_name", chp._chapter_name);

            std::vector<std::string> lines;
            ns_util::stringUtil::splitString(chp._text, &lines, "\n");
            for (const auto &line : lines)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("chapter_text");
                sub->SetValue("line", line);
            }

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 完成渲染功能
            tpl->Expand(html, &root);
        }
    };
}